package nl.hu.lcv.agents;

import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
import jade.wrapper.StaleProxyException;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;



/**
 * The Class ProductPoeper. Create productagents for testing
 */
public class ProductPoeper extends Agent
{
	public int ProductNumber = 1;
	private class ProductStartBehaviour extends OneShotBehaviour
	{	

		@Override
		public void action() {
			Random r = new Random();
			
			ArrayList<String> arr = new ArrayList<String>();
			for(int i = 0; i< 16; i++)
			{
				FileReader fr = null;
				BufferedReader br = null;
				try {
					fr = new FileReader("/home/lcv/low_cost_vision_svn/low-cost-vision-2012/Agents/Agent framework/product/place" + i + ".xml");
				
					br = new BufferedReader(fr);
					String xmlstring ="";
					String s;
					while((s = br.readLine()) != null) {
						xmlstring+=s;
						
					}
					arr.add(xmlstring);
				
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally
				{
					try {
						fr.close();
						br.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				
			
			}
			
			String ia[] = new String[arr.size()];
			ia = arr.toArray(ia);
			System.out.println(arr.get(0));
			
			Product p = new Product("P"+ProductNumber, ia, r.nextInt(200) + arr.size());			
			
			AgentContainer c = getContainerController();
	        try {
				AgentController a = c.createNewAgent( p.getName(), "nl.hu.lcv.agents.ProductAgent", new Object[]{p});
				a.start();
	        } catch (StaleProxyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}				
	        
			
		}
	
		
	}
	/** The Product number. */
	
	
	
	public void setup()
	{		
		addBehaviour(new ProductStartBehaviour());
	}
	
	

		
		
	
}
